package org.example.friend_test.controller;

import org.example.friend_test.entity.Couple;
import org.example.friend_test.service.CoupleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/couple")
public class CoupleController {
    
    @Autowired
    private CoupleService coupleService;
    
    // 创建情侣关系
    @PostMapping("/create")
    @ResponseBody
    public Map<String, Object> createCouple(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Long friend1Id = Long.parseLong(request.get("friend1Id").toString());
            Long friend2Id = Long.parseLong(request.get("friend2Id").toString());
            String startDate = request.get("startDate").toString();
            
            boolean success = coupleService.createCouple(friend1Id, friend2Id, startDate);
            
            if (success) {
                response.put("success", true);
                response.put("message", "情侣关系创建成功");
            } else {
                response.put("success", false);
                response.put("message", "创建失败：朋友已在情侣关系中");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "创建失败：" + e.getMessage());
        }
        
        return response;
    }
    
    // 获取所有活跃的情侣关系
    @GetMapping("/api/list")
    @ResponseBody
    public Map<String, Object> getAllCouples() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<Couple> couples = coupleService.getAllActiveCouples();
            response.put("success", true);
            response.put("data", couples);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
        }
        
        return response;
    }
    
    // 根据朋友ID获取情侣关系
    @GetMapping("/api/by-friend/{friendId}")
    @ResponseBody
    public Map<String, Object> getCouplesByFriend(@PathVariable Long friendId) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<Couple> couples = coupleService.getCouplesByFriendId(friendId);
            response.put("success", true);
            response.put("data", couples);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
        }
        
        return response;
    }
    
    // 结束情侣关系
    @PostMapping("/end/{id}")
    @ResponseBody
    public Map<String, Object> endCouple(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean success = coupleService.endCouple(id);
            
            if (success) {
                response.put("success", true);
                response.put("message", "情侣关系已结束");
            } else {
                response.put("success", false);
                response.put("message", "操作失败");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "操作失败：" + e.getMessage());
        }
        
        return response;
    }
    
    // 删除情侣关系
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> deleteCouple(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean success = coupleService.deleteCouple(id);
            
            if (success) {
                response.put("success", true);
                response.put("message", "情侣关系已删除");
            } else {
                response.put("success", false);
                response.put("message", "删除失败");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "删除失败：" + e.getMessage());
        }
        
        return response;
    }
    
    // 页面跳转
    @GetMapping("/list")
    public String coupleListPage() {
        return "redirect:/couple-list.html";
    }
    
    @GetMapping("/create")
    public String createCouplePage() {
        return "redirect:/create-couple.html";
    }
}
